package art.yulang.server;


import art.yulang.container.Context;
import lombok.Data;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装为Request对象（根据InputSteam输入流封装）
 */
@Data
public class Request {

    private String method; // 请求方式，比如GET/POST
    private String url;  // 例如 /,/index.html

    private InputStream inputStream;  // 输入流，其他属性从输入流中解析出来

    private String host; //请求的主机名称
    private String contextName; //请求上下文名称
    private String port;
    private Context context;

    public Request() {
    }

    // 构造器，输入流传入
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        // 获取第一行请求头信息
        String firstLineStr = inputStr.split("\\n")[0];  // GET / HTTP/1.1
        String secondLineStr = inputStr.split("\\n")[1];

        String[] strings = firstLineStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];

        String[] s1 = secondLineStr.split(" ");
        String[] split1 = s1[1].split(":");
        this.host = split1[0];
    }
}
